package ru.arriah.servicedesk.web.chief.action;

import java.io.IOException;
import java.util.Collection;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.arriah.common.web.action.ActionMapping;
import ru.arriah.common.web.action.DumbAction;
import ru.arriah.servicedesk.bean.ClientBean;
import ru.arriah.servicedesk.bean.DepartmentBean;
import ru.arriah.servicedesk.bean.GroupBean;
import ru.arriah.servicedesk.bean.OrganizationBean;
import ru.arriah.servicedesk.bean.RequestBean;
import ru.arriah.servicedesk.bean.RequestPriorityBean;
import ru.arriah.servicedesk.bean.RequestTypeBean;
import ru.arriah.servicedesk.ejb.ClientManagerLocalHome;
import ru.arriah.servicedesk.ejb.ClientManagerLocalObject;
import ru.arriah.servicedesk.ejb.GroupManagerLocalHome;
import ru.arriah.servicedesk.ejb.GroupManagerLocalObject;
import ru.arriah.servicedesk.ejb.OrganizationManagerLocalHome;
import ru.arriah.servicedesk.ejb.OrganizationManagerLocalObject;
import ru.arriah.servicedesk.ejb.RequestManagerLocalHome;
import ru.arriah.servicedesk.ejb.RequestManagerLocalObject;
import ru.arriah.servicedesk.help.Utils;
import ru.arriah.servicedesk.web.commonaction.exception.InternalException;

public class DisplayRequestAddFormAction extends DumbAction {
  protected RequestManagerLocalHome requestManagerHome;
  protected GroupManagerLocalHome groupManagerHome;
  protected OrganizationManagerLocalHome organizationManagerHome;
  protected ClientManagerLocalHome clientManagerHome;
  
  public DisplayRequestAddFormAction() throws NamingException {
    super();
    
    clientManagerHome = 
       Utils.getClientManagerHomeInterface();
    
    requestManagerHome =
       Utils.getRequestManagerHomeInterface();
    
    groupManagerHome =
      Utils.getGroupManagerHomeInterface();
    
    organizationManagerHome =
      Utils.getOrganizationManagerHomeInterface();
  }
  
  
  public String execute(
    ActionMapping actionMapping,
    HttpServletRequest request,
    HttpServletResponse response
  ) throws IOException, ServletException {
    try {
      RequestManagerLocalObject requestManager = 
        requestManagerHome.create();
      
      RequestBean requestBean = new RequestBean();
      requestBean.setPriority(3);
      
      OrganizationManagerLocalObject organizationManager = 
        organizationManagerHome.create();
      
      GroupManagerLocalObject groupManager = 
        groupManagerHome.create();
      
      ClientManagerLocalObject clientManager = 
        clientManagerHome.create();             
      
      Collection<GroupBean> groupsList = 
        groupManager.selectGroups();
      
      Collection<OrganizationBean> organizationsList = 
        organizationManager.selectOrganizations();
      
      //TODO replace to actual organization id
      Collection<DepartmentBean> dapartmentsList = 
         organizationManager.selectDepartments(1);
      
      Collection<RequestTypeBean> requestTypesList =       
        requestManager.selectRequestTypes();
      
      Collection<ClientBean> clientsList = 
        clientManager.selectClients();
      
      Collection<RequestPriorityBean> priorityList = 
        requestManager.selectPriorityList();
      
      requestManager.remove();
      organizationManager.remove();
      groupManager.remove();
      clientManager.remove();
      
      request.setAttribute("requestBean", requestBean);
      request.setAttribute("priorityList", priorityList);
      request.setAttribute("clientsList", clientsList);
      request.setAttribute("requestTypesList", requestTypesList);
      request.setAttribute("groupsList", groupsList);
      request.setAttribute("organizationsList", organizationsList);
      request.setAttribute("departmentsList", dapartmentsList);
      
      return actionMapping.getSuccessTarget();
    } catch (Exception e) {
      handleException(e, "execute");
      throw new InternalException("ErrorMessage.internal");
    }
  }
}